package pogvue.gui;

import pogvue.datamodel.GappedSequence;
import pogvue.datamodel.SequenceI;
import pogvue.gui.schemes.ColourSchemeI;

import java.awt.*;
import java.util.Hashtable;
import java.util.Vector;

public final class GraphRenderer implements RendererI {
    protected final Color   color = Color.black;
    protected final Color   maroon = new Color(200,0,50);
    private final Hashtable colhash = new Hashtable();
  public int os = 0;
    public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i) {
	Color c = cs.findColour(seq,seq.getSequence(i,i+1),i,null);
	return c;
    }
    
    public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText,Vector pid, int seqnum,AlignViewport av, Hashtable props, int[][] intpid) {
        Graphics2D g2d = (Graphics2D)g;
	
	int i      = start;
	int length = seq.getLength();

	Color currentColor = Color.white;

	g.setColor(Color.black);

        int prev  = -1;
	int pixel = -1;

	Vector inserts = null;

	//System.out.println("Drawing GraphRenderer");

	if (seq instanceof GappedSequence) {
	    inserts = ((GappedSequence)seq).inserts;
	}

	int    baseline       = av.getPIDBaseline();

	double window_mult = 100.0/(av.getPIDWindow()+1);
	double height_mult = height/(100.0-baseline);
	double width_mult  = width/2.0 + os;

	// Don't draw above a certain zoom level

	if (width <= .001) {
	    return;
	}

	Color cc = Color.lightGray;
	if (seq.getName().indexOf("ucsc") == 0) {
	    cc = Color.yellow;
	}

	while (i <= end && i < length) {

	  int newpixel = x1 + (int)(width*(i-start));

	  if (pixel == -1 || newpixel > pixel ||
	      i == end) {

	      char c = seq.getCharAt(i);

	      //System.out.println("Seuqence " + seq + " " + c);
            if (c != '-') {
	      //Vector values = (Vector)pid.elementAt(i-start);
	      int val =0;
	      if (intpid != null) {
		val   = intpid[i-start][seqnum];
	      }
	      //int val       = ((Integer)values.elementAt(seqnum)).intValue();
	      
		int frac = (int)(val*window_mult); 
	      
		frac -= baseline;
	      
		if (frac < 0) {
		    frac = 0;
		}
		if (baseline == 100) {
		    baseline = 99;
		}
		
		//g2d.setColor(Color.pink);
		g2d.setColor(cc);
	      
		int frac_tot = (int)(frac*height_mult);
	      
	      g.fillRect(newpixel,
			 y1 + height - frac_tot,
			 (int)width+1,       
			 frac_tot);
	      
	      g.setColor(Color.black);
	      
	      //	      if (prev == baseline || y1+height-frac_tot != prev) {
		  if (prev != -1) {
		      
		      g.drawLine(x1+(int)(width*(i-start)-width_mult),  prev,
				 x1+(int)(width*(i-start+1)-width_mult),y1+height - frac_tot);
		  } else {
		      g.drawLine(x1+(int)(width*(i-start))+os,  y1+height - frac_tot,
				 x1+(int)(width*(i-start+1))+os,y1+height - frac_tot);
		  }
		  prev = y1 + height - frac_tot;
		  //}
	    }
	  }
	  
	  pixel = newpixel;
	
	  i++;
	} 



        os = 0;

	int j = 0;


    }

    public Color getColor(int val) {
	if (!colhash.containsKey(new Integer(val))) {
	    Color tmp = new Color(val,0,0);
	    colhash.put(val,tmp);
	}

	return (Color)colhash.get(new Integer(val));
    }
}
